/**
    package: learn-go
    filename: main
    author: diogo@gmail.com
    time: 2022/9/19 15:23
**/

package main

import (
	"fmt"
	"strconv"
)

func main() {
	/**
	 * 非缓冲 make(chan T)
	 * 缓冲 make(chan T, capacity)
	 * 发送： 缓冲区的数据满了，才会阻塞
	 * 接受： 缓冲区的数据空了，才会阻塞
	 */

	//ch1 := make(chan int)
	//fmt.Println(len(ch1), cap(ch1))
	//ch1 <- 100
	//
	//ch2 := make(chan int, 5)
	//fmt.Println(len(ch2), cap(ch2))
	//
	//ch2 <- 100
	//fmt.Println(len(ch2), cap(ch2))

	ch3 := make(chan string, 4)

	go sendData4(ch3)

	for {
		v, ok := <-ch3
		if !ok {
			fmt.Println("读完了...", ok)
			break
		}
		fmt.Println("读取的数据是：", v)
	}

	fmt.Println("main over")
}

//sendData struct
func sendData4(ch chan string) {

	for i := 0; i < 10; i++ {

		ch <- "data" + strconv.Itoa(i)

		fmt.Printf("\n 子gorountine第%d个数据 \n ", i)
	}
	close(ch)
}
